package model.workersWithData;

import java.util.ArrayList;
import java.util.Date;

import org.apache.log4j.Logger;

import api.entitites.IGarage;
import api.entitites.IMaster;
import api.messages.Messager;

public class WorkerWithGarage {

 	private static ArrayList<String> arrayMessages= new ArrayList<String>();
	private static final Logger log = Logger.getLogger(WorkerWithGarage.class);

	
	public static ArrayList<String> getEmptyGarage(ArrayList<IGarage> garageList) {
		arrayMessages.clear();		
		if(!garageList.isEmpty()){
			Integer numberPlace=0;
			boolean temp = false;
			for (IGarage garage : garageList) {
				if (garage.getEmptyGarage()) {
					numberPlace++;
					temp = true;
				}
			}
			
			if (temp == false) {
				arrayMessages.add(Messager.ALL_GARAGES_BUSY);
			}
			
			arrayMessages.add(numberPlace+Messager.NUMBER_GARAGE);
			
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_GARAGE);

		}
		return arrayMessages;
	}

	
	public static ArrayList<String> getNumberEmptyPlaces(ArrayList<IMaster> masterList,
			ArrayList<IGarage> garageList, Date date) {
		arrayMessages.clear();
			Integer numberFreePlace=0;
			if (!garageList.isEmpty() && !masterList.isEmpty()) {
				ArrayList<IGarage> garageEmpty = new ArrayList<IGarage>();
				ArrayList<IMaster> masterFree = new ArrayList<IMaster>();

				for (IGarage garage : garageList) {
					if (garage.getOrder().getDeadlineDate().before(date)
							|| garage.getEmptyGarage()) {
						garageEmpty.add(garage);
					}
				}

				for (IMaster master : masterList) {
					if (master.getOrder().getDeadlineDate().before(date)
							|| master.getEmploymentMaster() == false) {
						masterFree.add(master);
					}
				}

				if (masterFree.size() > garageEmpty.size()) {
					numberFreePlace=garageEmpty.size();
				} else if (masterFree.size() < garageEmpty.size()) {
					numberFreePlace=masterFree.size();
				} else if (masterFree.size() == garageEmpty.size()) {
					numberFreePlace=garageEmpty.size();
				}
			} else if (garageList.isEmpty()) {
				arrayMessages.add(Messager.NOT_GARAGE);
			} else {
				numberFreePlace=garageList.size();
			}
			
			if(numberFreePlace!=0){
			arrayMessages.add(numberFreePlace+Messager.NUMBER_GARAGE_ON_DATE);	
			}else if(!garageList.isEmpty() && numberFreePlace==0){
				arrayMessages.add(Messager.NOT_EMPTY_PLACE_ON_DATE);
			}
				
			return arrayMessages;

		
	}

	
	public static ArrayList<String> addGarage(ArrayList<IGarage> garageList, IGarage garage) {
		arrayMessages.clear();
		try {
			int i = 0;
			for (IGarage g : garageList) {
				if (g.getNumberGarage() == garage.getNumberGarage()) {
					i++;
					arrayMessages.add(Messager.GARAGE_EXISTS);
				}
			}
			if (i == 0) {
				garageList.add(garage);
				arrayMessages.add(Messager.GARAGE_ADD);
				
			}
		} catch (NullPointerException n) {
			log.warn(Messager.EMPTY_COLLEC);

		}
		return arrayMessages;

	}

	
	public static ArrayList<String> deleteGarage(ArrayList<IGarage> garageList,
			Integer numberGarage) {
		arrayMessages.clear();

		try {
			ArrayList<IGarage> temp = new ArrayList<IGarage>();
			for (IGarage gar : garageList) {
				if (gar.getNumberGarage() != numberGarage) {
					temp.add(gar);

				}
			}

			if (temp.size() == garageList.size()) {
				arrayMessages.add(Messager.GARAGE_NOT_EXISTS);
			} else {
				garageList.clear();
				garageList.addAll(temp);
				arrayMessages.add(Messager.GARAGE_DELETE);
			}
		} catch (NullPointerException n) {
			log.warn(Messager.EMPTY_COLLEC);

		}
		return arrayMessages;

	}

}
